define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'kmh/voucher/index' + location.search,
                    add_url: 'kmh/voucher/add',
                    edit_url: 'kmh/voucher/edit',
                    del_url: 'kmh/voucher/del',
                    multi_url: 'kmh/voucher/multi',
                    import_url: 'kmh/voucher/import',
                    table: 'new_voucher',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'type', title: __('Type'),formatter: function (value) {
                                switch (value) {
                                    case 1:
                                        return '立减券';
                                    case 2:
                                        return '实物';
                                }
                            },searchList: {1:'立减券',2:'实物'}},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'),formatter: function (value) {
                                switch (value) {
                                    case 1:
                                        return '启用';
                                    case 0:
                                        return '停用';
                                }
                            },searchList: {1:'启用',2:'停用'}},
                        {field: 'sy_number', title: __('数量')},
                        // {field: 'send_num', title: __('发放数量'),operate:false},
                        // {field: 'total_num', title: __('总数量'),operate:false},
                        {field: 'limit_min', title: __('Limit_min'), operate:'BETWEEN'},
                        {field: 'limit_max', title: __('Limit_max'), operate:'BETWEEN'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $('input[type=radio][name="row[type]"]').change(function () {
                var value = $(this).val();
                if (value == 1) {
                    $('.ljx').show();
                } else {
                    $('.ljx').hide();
                }
            });
            Controller.api.bindevent();
        },
        edit: function () {
            if($('input[type=radio][name="row[type]"]:checked').val() == 1){
                $('.ljx').show();
            } else {
                $('.ljx').hide();
            }

            $('input[type=radio][name="row[type]"]').change(function () {
                var value = $(this).val();
                if (value == 1) {
                    $('.ljx').show();
                } else {
                    $('.ljx').hide();
                }
            });

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});